﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using YunShop_WebAPI.Model;

namespace YunShop_WebAPI.BLL
{   
    public class MiniMngCommon
    {
        public static string YunShopUrl = System.Configuration.ConfigurationManager.AppSettings["YunShopUrl"].ToString();

        /// <summary>
        /// 通过appid获取密钥
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static string GetAppSecret(string appid)
        {
            string sql = @"select FAppSecret from t_MiniSetting where FAppID=@FAppID";
            SqlParameterList parlist = new SqlParameterList();
            parlist.Add("@FAppID", SqlDbType.VarChar, appid);
            DataTable dt = DBHelper.GetDataTable(sql, parlist.Get_List(), DBHelper.ConnMaster);

            if (dt.Rows.Count > 0) {
                DataRow dr = dt.Rows[0];
                return CommonFun.ConvertToString(dr["FAppSecret"]);
            }
            return "";
        }

        /// <summary>
        /// 查询商家登录
        /// </summary>
        public static bool GetUserInfo(string appid,string code, ref string usercode, 
            ref string password,ref string openId, ref string message, string connstr)
        {
            try
            {
                var secret = GetAppSecret(appid);
                if (string.IsNullOrEmpty(secret))
                {
                    message = "secret不能为空";
                    return false;
                }

                //获取openId
                JObject jo = WxService.GetOpenIdByCode(code, appid, secret);
                var unionId = CommonFun.GetJObjectValue(jo, "unionid");
                openId = CommonFun.GetJObjectValue(jo, "openid");//orPaa5eBqlpEauuYCjEKPMnxpMa0

                if (string.IsNullOrEmpty(unionId) && string.IsNullOrEmpty(openId))
                {
                    message = "code解析失败";
                    return false;
                }

                if (!string.IsNullOrEmpty(usercode)) return true;

                string sql = @"SELECT b.FNumber, b.FPassword FROM dbo.t_UserWeixin AS a INNER JOIN
                      dbo.t_User AS b ON a.FUserID = b.FItemID where a.FOpenID=@FOpenID";
                SqlParameterList parlist = new SqlParameterList();
                parlist.Add("@FOpenID", SqlDbType.VarChar, openId);
                DataTable dt = DBHelper.GetDataTable(sql, parlist.Get_List(), connstr);

                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    usercode = CommonFun.ConvertToString(dr["FNumber"]);
                    password = CommonFun.ConvertToString(dr["FPassword"]);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 查询商家登录
        /// </summary>
        public static string GetLoginClusterIP(string usercode,string password,ref int sMasterFlag,ref string DbSplit)
        {
            try
            {
                sMasterFlag = 1;
                string sql = @"select FItemID,FCorpID from t_User 
			        where ((FNumber = @usercode) or ((FTelephone = @usercode) 
                    and (LEN(@usercode) = 11))) and (FPassword=@userpwd) and (@usercode <> '')";
                SqlParameterList parlist = new SqlParameterList();
                parlist.Add("@usercode", SqlDbType.VarChar, usercode);
                parlist.Add("@userpwd", SqlDbType.VarChar, password);
                DataTable dt = DBHelper.GetDataTable(sql, parlist.Get_List(), DBHelper.ConnMaster);

                if (dt.Rows.Count > 0) return DBHelper.HostMaster;
                string retDbHostIP = DBHelper.HostMaster;
                string msql = "Select * from t_CorpHost where FMasterSplit = 1";
                DataTable dt_host = DBHelper.GetDataTable(msql, DBHelper.ConnMaster);
                foreach (DataRow dr in dt_host.Rows)
                {
                    string sDbHostIP = CommonFun.ConvertToString(dr["FDbHostIP"]); ;  //"39.100.91.34";// CommonFun.ConvertToString(dr["FDbHostIP"]);
                    if (DBHelper.DebugFlag) sDbHostIP = CommonFun.ConvertToString(dr["FDbHostNetIP"]);
                    DbSplit = CommonFun.ConvertToString(dr["FDbSplit"]);
                    string connstr = string.Format(DBHelper.ConnCluster, sDbHostIP, DbSplit);
                    SqlParameterList parlist1 = new SqlParameterList();
                    parlist1.Add("@usercode", SqlDbType.VarChar, usercode);
                    parlist1.Add("@userpwd", SqlDbType.VarChar, password);
                    DataTable dt_user_Cluster = DBHelper.GetDataTable(sql, parlist1.Get_List(), connstr);
                    if ((dt_user_Cluster != null) && (dt_user_Cluster.Rows.Count > 0))
                    {
                        int sCorpID = CommonFun.ConvertToInt(dt_user_Cluster.Rows[0]["FCorpID"]);
                        msql = "Select * from t_CorpHost where FMinCorpID <= {0} and FMaxCorpID >= {0}";
                        msql = string.Format(msql, sCorpID);
                        DataTable dt_Host_Split = DBHelper.GetDataTable(msql, parlist1.Get_List(), connstr);
                        if (dt_Host_Split != null)
                        {
                            sMasterFlag = 0;
                            DbSplit = CommonFun.ConvertToString(dt_Host_Split.Rows[0]["FDbSplit"]);
                            return sDbHostIP;
                        }
                    }
                }
                return retDbHostIP;
            }
            catch (Exception ex)
            {                
                return DBHelper.HostMaster;
            }
        }

        /// <summary>
        /// 查询商家登录
        /// </summary>
        public static ArrayList OnUserLogin(string appid,string code, string usercode, string password,ref int status, ref string message)
        {
            int sMasterFlag = 1;
            var appModel = new SystemAppModel();
            string sPassword = Security.Encrypt(password);
            string sDbSplit = "";
            appModel.FHostIP = GetLoginClusterIP(usercode, sPassword, ref sMasterFlag,ref sDbSplit);
            string connstr = string.Format(DBHelper.ConnCluster, appModel.FHostIP, sDbSplit);
            string openid = "";
            if (!GetUserInfo(appid, code, ref usercode, ref sPassword,ref openid, ref message, connstr))
            {
                message = "code解析失败";
                return null;
            }
            string msql = "Exec pr_min_UserLogin @phone,@userpwd,@openID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@phone", SqlDbType.VarChar, usercode);
            par_list.Add("@userpwd", SqlDbType.VarChar, sPassword);
            par_list.Add("@openID", SqlDbType.VarChar, openid);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                if (CommonFun.ConvertToInt(ht["retvalue"]) > 0)
                {
                    status = 1;
                    message = "登录成功";
                    appModel.FCorpID = CommonFun.ConvertToInt(ht["FCorpID"]);//12
                    appModel.FUserID = CommonFun.ConvertToInt(ht["FUserID"]);
                    appModel.FStockID = CommonFun.ConvertToBigInt(ht["FStockID"]);
                    appModel.FOwnerID = CommonFun.ConvertToInt(ht["FOwnerID"]);
                    appModel.FParentID = CommonFun.ConvertToInt(ht["FParentID"]);
                    appModel.FModifyTime = CommonFun.ConvertToBigInt(ht["FModifyTime"]);
                    appModel.FLoginDate = DateTime.Now.Day;
                    msql = @"SELECT FDbHostIP,FDbSplit,FDbHostNetIP FROM t_CorpHost where FMinCorpID <= {0} and FMaxCorpID >= {0}";
                    if (appModel.FParentID > 0)
                        msql = string.Format(msql, appModel.FParentID);
                    else msql = string.Format(msql, appModel.FCorpID);

                    ArrayList dataAll = DBHelper.Select(msql, DBHelper.ConnMaster);
                    if (dataAll.Count > 0)
                    {
                        Hashtable record = (Hashtable)dataAll[0];
                        appModel.FHostIP = CommonFun.ConvertToString(record["FDbHostIP"]);
                        appModel.FDbSplit = CommonFun.ConvertToString(record["FDbSplit"]);
                        if (DBHelper.DebugFlag) appModel.FHostIP = CommonFun.ConvertToString(record["FDbHostNetIP"]);
                    }
                    string sSystemAppID = SystemInfo.SetSystemAppID(appModel);
                    ht["shop_appid"] = sSystemAppID;
                    return Utils.HashtableToArrayList(ht);
                }
                else
                {
                    status = 0;
                    message = CommonFun.ConvertToString(ht["msg"]);
                    return null;
                }
            }
            else return null;
        }

        /// <summary>
        /// 会员列表
        /// </summary>
        public static ArrayList GetMemberList(string shop_appid, string name, int page_index, int page_size, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_bi_GetMemberList @mPageSize,@mPageIndex,@mClassID,@mNumber,@mCorpID,0,0,0,'','',0,''";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mClassID", SqlDbType.BigInt, 0);
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mNumber", SqlDbType.VarChar, name);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);                
            }
            return datalist;
        }

        /// <summary>
        /// 会员列表
        /// </summary>
        public static Hashtable GetMemberInfo(string shop_appid, Int64 item_id)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_bi_GetMemberInfo @FCorpID,@FItemID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@FItemID", SqlDbType.BigInt, item_id);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist.Count > 0) {
                Hashtable ht_result = datalist[0] as Hashtable;
                ht_result["FSheFlag"] = CommonFun.ConvertIntToBoolean(ht_result["FSheFlag"]);
                return ht_result;
            }
            return null;
        }

        //获取表信息（所有字段）
        public static Hashtable GetInfo(string shop_appid, Int64 item_id, string tablename)
        {
            try
            {
                int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
                string connstr = SystemInfo.GetClusterConn(shop_appid);

                string msql = @"select a.* from " + tablename + " a where a.FItemID=@FItemID";
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@FItemID", SqlDbType.BigInt, item_id);
                ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
                if (datalist.Count > 0)
                {
                    return datalist[0] as Hashtable;
                }
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        /// <summary>
        /// 商品列表
        /// </summary>
        public static ArrayList GetGoodsList(string shop_appid,string name, int page_index, int page_size, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_bi_GetGoodsList @mPageSize,@mPageIndex,@mCorpID,@mClassID,@mNumber,0,@mName";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mClassID", SqlDbType.BigInt, 0);
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mNumber", SqlDbType.VarChar, name);
            par_list.Add("@mName", SqlDbType.VarChar, name);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);                
            }
            return datalist;
        }

        /// <summary>
        /// 商品排行
        /// </summary>
        public static ArrayList RptGoodsSort(string shop_appid, int shop_id, string name, int page_index, int page_size,
            string star_date, string end_date, int type, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);

            string msql = @"EXEC dbo.pr_Rpt_Pos_GetGoodsSum_Ex @mPageSize,@mPageIndex,@mDateTime1,"
                    + "@mDateTime2,@mCorpID,@mStockID,0,0,0,0,0,0,@mNumber,@mSort";
            
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mDateTime1", SqlDbType.VarChar, CommonFun.ConvertToDate(star_date) + " 00:00:01");
            par_list.Add("@mDateTime2", SqlDbType.VarChar, CommonFun.ConvertToDate(end_date) + " 23:59:59");
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mStockID", SqlDbType.Int, shop_id);
            par_list.Add("@mNumber", SqlDbType.VarChar, name);
            par_list.Add("@mSort", SqlDbType.Int, type);           
            
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 收银汇总
        /// </summary>
        public static ArrayList RptCashSummary(string shop_appid, int shop_id,string bill_no, int page_index, int page_size,
            string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "EXEC dbo.pr_Pos_GetClassDataSum @mPageSize,@mPageIndex,@mDateTime1,@mDateTime2,"
                    + "@mCorpID,@mStockID,0,@mBillNo";

            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mDateTime1", SqlDbType.VarChar, CommonFun.ConvertToDate(star_date) +" 00:00:01");
            par_list.Add("@mDateTime2", SqlDbType.VarChar, CommonFun.ConvertToDate(end_date) + " 23:59:59");
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mStockID", SqlDbType.Int, shop_id);
            par_list.Add("@mBillNo", SqlDbType.VarChar, bill_no);

            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return GetCashSummary(datalist, mCorpID);
        }

        /// <summary>
        /// 收银明细
        /// </summary>
        public static ArrayList RptCashDetail(string shop_appid, int shop_id, string bill_no, int page_index, int page_size,
            string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "EXEC dbo.pr_Rpt_GetBllDetail_Pos @mPageSize,@mPageIndex,@mDateTime1,@mDateTime2,"
                    + "@mCorpID,0,0,0,0,0,0,'',@mBillNo,0";

            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mDateTime1", SqlDbType.VarChar, CommonFun.ConvertToDate(star_date) + " 00:00:01");
            par_list.Add("@mDateTime2", SqlDbType.VarChar, CommonFun.ConvertToDate(end_date) + " 23:59:59");
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mBillNo", SqlDbType.VarChar, bill_no);

            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 收银汇总数据处理
        /// </summary>
        public static ArrayList GetCashSummary(ArrayList datalist,int mCorpID)
        {
            bool bAmountFlag = false;
            string connstr = SystemInfo.GetClusterConn(mCorpID); ;
            ArrayList pay_list = new ArrayList();
            foreach (Hashtable row in datalist)
            {
                bAmountFlag = false;
                Hashtable ht_detail = new Hashtable();
                ht_detail["FClassNo"] = CommonFun.ConvertToString(row["FClassNo"]);
                ht_detail["FBeginTime"] = CommonFun.ConvertToString(row["FBeginTime"]);
                ht_detail["FEndTime"] = CommonFun.ConvertToString(row["FEndTime"]);
                ht_detail["FPayList"] = GetCashSummaryPay(row, mCorpID,ref bAmountFlag, connstr);
                if (bAmountFlag) pay_list.Add(ht_detail);
            }
            return pay_list;
        }

        /// <summary>
        /// 收银汇总支付明细
        /// </summary>
        public static ArrayList GetCashSummaryPay(Hashtable row,int mCorpID,ref bool bAmountFlag,string shop_appid)
        {
            ArrayList pay_list = new ArrayList();
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"Declare @FName20 varchar(50)
                select @FName20 = FName from t_Payment where FClassID = 20 and FCorpID = {0}
                select FItemID,FName,FieldName,@FName20 as FName20 from t_PaymentClass 
                where FieldName <> 'FPayAmount6' or FItemID = 6 order by FItemID";
            msql = string.Format(msql, mCorpID);
            ArrayList field_list = DBHelper.Select(msql,connstr);

            decimal mPayAmount = 0;
            foreach (Hashtable ht in field_list)
            {
                mPayAmount = 0;
                string FieldName = CommonFun.ConvertToString(ht["FieldName"]);
                string PayFName = CommonFun.ConvertToString(ht["FName"]);
                if ((row.Contains(FieldName)) && (CommonFun.ConvertToDecimal(row[FieldName]) != 0))
                {
                    if (CommonFun.ConvertToInt(ht["FItemID"]) == 20)
                    {
                        string CustFName = CommonFun.ConvertToString(ht["FName20"]);
                        if (!string.IsNullOrEmpty(CustFName)) PayFName = CustFName;
                    }
                    Hashtable ht_detail = new Hashtable();
                    ht_detail.Add("FPayName", PayFName);
                    ht_detail.Add("FAmount", CommonFun.ConvertToDecimal(row[FieldName]).ToString("#0.00"));
                    pay_list.Add(ht_detail);
                    mPayAmount += CommonFun.ConvertToDecimal(row[FieldName]);
                }
            }
            if (mPayAmount != 0) bAmountFlag = true;
            return pay_list;
        }

        /// <summary>
        /// 会员充值消费明细
        /// </summary>
        public static ArrayList RptRechargeRecord(string shop_appid, int shop_id, int type, string phone, int page_index, int page_size,
            string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "exec dbo.pr_Rpt_GetMemberAccAmt @mPageSize,@mPageIndex,@mCorpID,@FStockID," +
                "@FPayID,@FBillNo,@FMemberItemID,@FUserID,@FMemberTimeID,@FDate1,@FDate2,@FDataType,@FClassType";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, 4, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, 4, page_index);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@FStockID", SqlDbType.BigInt, shop_id);
            par_list.Add("@FPayID", SqlDbType.Int, -1);
            par_list.Add("@FBillNo", SqlDbType.VarChar, "");
            par_list.Add("@FMemberItemID", SqlDbType.BigInt, 0);
            par_list.Add("@FUserID", SqlDbType.BigInt, 0);
            par_list.Add("@FMemberTimeID", SqlDbType.BigInt, 0);
            par_list.Add("@FDate1", SqlDbType.VarChar, star_date + " 00:00:01");
            par_list.Add("@FDate2", SqlDbType.VarChar, end_date + " 23:59:59");
            par_list.Add("@FDataType", SqlDbType.Int, type);
            par_list.Add("@FClassType", SqlDbType.Int, 0);

            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 门店综合报表
        /// </summary>
        public static ArrayList RptStockAnalysis(string shop_appid,int shop_id, int page_index, int page_size, string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "exec dbo.pr_Rpt_StockAnalysis @mPageSize,@mPageIndex,@mDate1,@mDate2,@mCorpID,@mStockID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, 4, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, 4, page_index);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mStockID", SqlDbType.BigInt, shop_id);
            par_list.Add("@mDate1", SqlDbType.VarChar, star_date + " 00:00:01");
            par_list.Add("@mDate2", SqlDbType.VarChar, end_date + " 23:59:59");
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 收款分析
        /// </summary>
        public static ArrayList RptPayAnalysis(string shop_appid, int shop_id, string star_date, string end_date)
        {            
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "exec dbo.pr_Rpt_GetPayAnalysis @mDate1,@mDate2,@mStockID,@mCorpID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mStockID", SqlDbType.BigInt, shop_id);
            par_list.Add("@mDate1", SqlDbType.VarChar, star_date + " 00:00:01");
            par_list.Add("@mDate2", SqlDbType.VarChar, end_date + " 23:59:59");
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            return datalist;
        }

        /// <summary>
        /// 销售分析
        /// </summary>
        public static Hashtable RptBuyingAnalysis(string shop_appid, int shop_id, int page_index, int page_size, string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            Hashtable ht_result = new Hashtable();
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "exec dbo.pr_Rpt_min_GetBuyingAnalysis @mPageSize,@mPageIndex,@mDate1,@mDate2,@mCorpID,@mStockID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, 4, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, 4, page_index);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mStockID", SqlDbType.BigInt, shop_id);
            par_list.Add("@mDate1", SqlDbType.VarChar, star_date);
            par_list.Add("@mDate2", SqlDbType.VarChar, end_date);
            DataSet ds = DBHelper.GetDataSet(msql, par_list.Get_List(),connstr);
            if (ds != null && ds.Tables.Count < 2) return null;

            ArrayList Detail = DBHelper.DataTable2ArrayList(ds.Tables[0]);
            ht_result["detail"] = Detail;
            ArrayList chart = DBHelper.DataTable2ArrayList(ds.Tables[1]);
            ht_result["dchart"] = chart;

            DataTable total = ds.Tables[2];
            if (total.Rows.Count > 0)
            {
                ArrayList list_total = DBHelper.DataTable2ArrayList(ds.Tables[2]);
                ht_result["total"] = list_total[0] as Hashtable;
            }
            else ht_result["total"] = null;
            return ht_result;
        }

        /// <summary>
        /// 销售分类
        /// </summary>
        public static Hashtable RptMarketAnalysis(string shop_appid, int shop_id, int page_index, int page_size, string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            Hashtable ht_result = new Hashtable();
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "exec dbo.pr_Rpt_Pos_GetStockSort @mPageSize,@mPageIndex,@mDateTime1,@mDateTime2,@mCorpID,@mStockID,@mChartFlag";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, 4, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, 4, page_index);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mStockID", SqlDbType.BigInt, shop_id);
            par_list.Add("@mDateTime1", SqlDbType.VarChar, star_date);
            par_list.Add("@mDateTime2", SqlDbType.VarChar, end_date);
            par_list.Add("@mChartFlag", SqlDbType.Int, 1);
            DataSet ds = DBHelper.GetDataSet(msql, par_list.Get_List(), connstr);
            if (ds != null && ds.Tables.Count < 2) return null;

            ArrayList Detail = DBHelper.DataTable2ArrayList(ds.Tables[0]);
            ht_result["detail"] = Detail;
            ArrayList chart = DBHelper.DataTable2ArrayList(ds.Tables[1]);
            ht_result["dchart"] = chart;

            if (Detail != null && Detail.Count > 0)
            {
                Hashtable ht = Detail[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return ht_result;
        }

        /// <summary>
        /// 导购员绩效
        /// </summary>
        public static Hashtable RptDawEmploy(string shop_appid, int shop_id,int type, int page_index, 
            int page_size, string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            Hashtable ht_result = new Hashtable();
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            Int64 mEmpID = 0;
            if (type == 1) mEmpID = MniMngService.GetEmployeeID(shop_appid);
            string msql = "EXEC dbo.pr_Rpt_GetDawEmploy @mPageSize,@mPageIndex,@FDate1,@FDate2,@FCorpID,@FStockID,@FEmpID,@mChartFlag";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, 4, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, 4, page_index);
            par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@FStockID", SqlDbType.BigInt, shop_id);
            par_list.Add("@FDate1", SqlDbType.VarChar, star_date);
            par_list.Add("@FDate2", SqlDbType.VarChar, end_date);
            par_list.Add("@FEmpID", SqlDbType.Int, mEmpID);
            par_list.Add("@mChartFlag", SqlDbType.Int, 1);
            DataSet ds = DBHelper.GetDataSet(msql, par_list.Get_List(), connstr);
            if (ds != null && ds.Tables.Count < 2) return null;

            ArrayList Detail = DBHelper.DataTable2ArrayList(ds.Tables[0]);
            ht_result["detail"] = Detail;
            ArrayList chart = DBHelper.DataTable2ArrayList(ds.Tables[1]);
            ht_result["dchart"] = chart;

            if (Detail != null && Detail.Count > 0)
            {
                Hashtable ht = Detail[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return ht_result;
        }

        /// <summary>
        /// 导购明细
        /// </summary>
        public static ArrayList RptDawEmpDetail(string shop_appid, int shop_id, int type, int page_index,
            int page_size, string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            Int64 mEmpID = 0;
            if (type == 1) mEmpID = MniMngService.GetEmployeeID(shop_appid);
            string msql = "EXEC dbo.pr_Rpt_GetDawEmpDetail @mPageSize,@mPageIndex,@mDate1,@mDate2,@mDataType,@mCorpID,@mStockID,@FEmpID,0,0";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, 4, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, 4, page_index);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mStockID", SqlDbType.BigInt, shop_id);
            par_list.Add("@mDate1", SqlDbType.VarChar, star_date);
            par_list.Add("@mDate2", SqlDbType.VarChar, end_date);
            par_list.Add("@FEmpID", SqlDbType.Int, mEmpID);
            par_list.Add("@mDataType", SqlDbType.Int, 0);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            
            if (datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 交易分析
        /// </summary>
        public static ArrayList RptBuyingFlow(string shop_appid, int shop_id, int page_index, int page_size, string star_date, string end_date, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "EXEC dbo.pr_Rpt_Flow @PageSize,@PageIndex,@FCorpID,@FStockID,@StartDate,@EndDate,@DayOrMonth";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@StartDate", SqlDbType.VarChar, star_date);
            par_list.Add("@EndDate", SqlDbType.VarChar, end_date);
            par_list.Add("@PageIndex", SqlDbType.Int, page_index);
            par_list.Add("@PageSize", SqlDbType.Int, page_size);
            par_list.Add("@FStockID", SqlDbType.Int, shop_id);
            par_list.Add("@DayOrMonth", SqlDbType.Int, 10);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 客流时段分析
        /// </summary>
        public static ArrayList RptAnalysisTime(string shop_appid, int shop_id, string star_date, string end_date)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "EXEC pr_Rpt_Pos_GetAnalysisTime @mDateTime1,@mDateTime2,@mCorpID,@mStockID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mDateTime1", SqlDbType.VarChar, star_date);
            par_list.Add("@mDateTime2", SqlDbType.VarChar, end_date);
            par_list.Add("@mStockID", SqlDbType.Int, shop_id);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            return datalist;
        }

        /// <summary>
        /// 门店营业概况
        /// </summary>
        public static Hashtable RptBusinessDaily(string shop_appid, int shop_id)
        {
            Hashtable ht_result = new Hashtable();
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "EXEC dbo.pr_min_GetBusinessDaily @mCorpID,@mStockID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mStockID", SqlDbType.BigInt, shop_id);

            DataSet ds = DBHelper.GetDataSet(msql, par_list.Get_List(),connstr);
            if (ds != null && ds.Tables.Count < 2) return null;

            DataTable dt_today = ds.Tables[0];     
            if (dt_today.Rows.Count > 0)
            {
                ArrayList list_today = DBHelper.DataTable2ArrayList(ds.Tables[0]);
                ht_result["data_today"] = list_today[0] as Hashtable;
            }
            else ht_result["data_today"] = null;

            ArrayList data_week = DBHelper.DataTable2ArrayList(ds.Tables[1]);
            ht_result["data_week"] = data_week;            

            return ht_result;
        }

        /// <summary>
        /// 采购入库列表
        /// </summary>
        public static ArrayList GetWarehousList(string shop_appid, string name, int page_index, int page_size,
            string star_date,string end_date,int datatype, int checkflag, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_Rpt_GetBllList_Ins @mPageSize,@mPageIndex,@mDate1,"
                    + "@mDate2,@mCorpID,@mCheckFlag,@mBillType,0,0,@mBillNo";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mDate1", SqlDbType.VarChar, CommonFun.ConvertToDate(star_date));
            par_list.Add("@mDate2", SqlDbType.VarChar, CommonFun.ConvertToDate(end_date));
            par_list.Add("@mCheckFlag", SqlDbType.Int, checkflag);
            par_list.Add("@mBillType", SqlDbType.Int, datatype);
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mBillNo", SqlDbType.VarChar, name);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);                
            }
            return datalist;
        }

        /// <summary>
        /// 库存盘点列表
        /// </summary>
        public static ArrayList GetInventoryList(string shop_appid, string name, int page_index, int page_size,
            string star_date, string end_date, int checkflag, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_Rpt_GetBllList_Stc @mPageSize,@mPageIndex,@mDate1,"
                    + "@mDate2,@mCorpID,@mCheckFlag,@mStockID,@mRemark,@mContent,@mBillNo";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mDate1", SqlDbType.VarChar, CommonFun.ConvertToDate(star_date));
            par_list.Add("@mDate2", SqlDbType.VarChar, CommonFun.ConvertToDate(end_date));
            par_list.Add("@mCheckFlag", SqlDbType.Int, checkflag);
            par_list.Add("@mStockID", SqlDbType.Int, 0);
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mRemark", SqlDbType.VarChar, "");
            par_list.Add("@mContent", SqlDbType.VarChar, "");
            par_list.Add("@mBillNo", SqlDbType.VarChar, "");
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 采购入库列表
        /// </summary>
        public static ArrayList GetMallOrderList(string shop_appid, int shop_id, string name, int page_index, int page_size,
            string star_date, string end_date, int bill_type, int bill_state, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_Rpt_GetBllList_MallOd @mPageSize,@mPageIndex,@mDate1,@mDate2,"
                    +"@mCorpID,@mCheckFlag,@mBillType,@mStockID,@mObjectID,@mBillNo,@mBillStateID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mDate1", SqlDbType.VarChar, CommonFun.ConvertToDate(star_date) + " 00:00:01");
            par_list.Add("@mDate2", SqlDbType.VarChar, CommonFun.ConvertToDate(end_date) + " 23:59:59");
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mCheckFlag", SqlDbType.Int, 0);
            par_list.Add("@mBillType", SqlDbType.Int, bill_type);
            par_list.Add("@mStockID", SqlDbType.Int, shop_id);
            par_list.Add("@mObjectID", SqlDbType.Int, 0);
            par_list.Add("@mBillStateID", SqlDbType.Int, bill_state);
            par_list.Add("@mBillNo", SqlDbType.VarChar, name);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }


        /// <summary>
        /// 供应商列表
        /// </summary>
        public static ArrayList GetSupplierList(string shop_appid, string name, int page_index, int page_size, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_bi_GetSupplierList @mPageSize,@mPageIndex,@mCorpID,@mName";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mName", SqlDbType.VarChar, name);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);                
            }
            return datalist;
        }

        /// <summary>
        /// 单据商品选择
        /// </summary>
        public static ArrayList GetBillGoods(string shop_appid, string name,Int64 item_id, int type, 
            int page_index, int page_size, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);

            string msql = @"EXEC pr_ic_GetBillGoods	@mPageSize,@mPageIndex,@mCorpID,0,@mBillType,0,"
                + "@mNumber,-1,0,@mFItemID,@mFromType";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mBillType", SqlDbType.Int, type);
            par_list.Add("@mNumber", SqlDbType.VarChar, name);
            par_list.Add("@mFItemID", SqlDbType.BigInt, item_id);
            par_list.Add("@mFromType", SqlDbType.Int, 2);//2为小程序端，要显示图片
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);                
            }
            return datalist;
        }

        /// <summary>
        /// 商品列表
        /// </summary>
        public static ArrayList GetStockTotal(string shop_appid,string name, int page_index, int page_size, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_Rpt_GetStockTotal @mPageSize,@mPageIndex,@mCorpID,0,0,0,@mNumber,@mDataType,0";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mNumber", SqlDbType.VarChar, name);
            par_list.Add("@mDataType", SqlDbType.Int, -1);//小程序端，不显示合计栏
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = BLL.Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);                
            }
            return datalist;
        }

        /// <summary>
        /// 次卡结存
        /// </summary>
        public static ArrayList RptTimeInventory(string shop_appid, Int64 time_id,Int64 member_id,
            int page_index, int page_size, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_Rpt_GetMemberTimeTotal @mPageSize,@mPageIndex,"
                    + "@mCorpID,@mStockID,@mMemberID,@mMemberTimeID,@mDataType";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mStockID", SqlDbType.BigInt, 0);
            par_list.Add("@mMemberID", SqlDbType.BigInt, member_id);
            par_list.Add("@mMemberTimeID", SqlDbType.BigInt, time_id);
            par_list.Add("@mDataType", SqlDbType.VarChar, 0);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = BLL.Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 商品保存
        /// </summary>
        public static Hashtable SetGoodsInfo(Model.GoodsModel goodsModel, string shop_appid,ref int mResult,ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Goods/SaveGoodsInfo";
                string param = "json={0}&specjson=&colorjson=&pricejson=&mSystemAppID={1}";
                string mjson = MniMngService.GoodsModelToStr(goodsModel);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 商品保存
        /// </summary>
        public static Hashtable SetMemberInfo(Model.MemberModel memberModel, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Member/SaveInfo";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.MemberModelToStr(memberModel);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch {  }
            return null;
        }

        /// <summary>
        /// 会员分类保存
        /// </summary>
        public static Hashtable SetMemberType(Model.MemberTypeModel mtypeModel, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/MemberClass/SaveInfo";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.MemberTypeModelToStr(mtypeModel);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 商品分类保存
        /// </summary>
        public static Hashtable SetGoodsType(Model.GoodsTypeModel gtypeModel, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/GoodClass/saveGoodClass";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.GoodsTypeModelToStr(gtypeModel,0);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 用户信息保存
        /// </summary>
        public static Hashtable SetUserInfo(Hashtable row, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                Int64 UserID = CommonFun.ConvertToBigInt(row["FItemID"]);
                UserModel userModel = new UserModel();
                if (UserID > 0) userModel.FItemID = CommonFun.ConvertToString(UserID);
                userModel.FName = CommonFun.ConvertToString(row["FName"]);
                userModel.FType = CommonFun.ConvertToInt(row["FType"]);
                userModel.FPassword = CommonFun.ConvertToString(row["FPassword"]);
                userModel.FTelephone = CommonFun.ConvertToString(row["FTelephone"]);

                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/User/SaveUserInfo";
                //url = "http://localhost:13065/User/SaveUserInfo";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.UserModelToStr(userModel);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 用户信息保存
        /// </summary>
        public static Hashtable SetCashierAuthority(Hashtable row, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                UserPowerModel userPower = new UserPowerModel();
                userPower.FUserID = CommonFun.ConvertToBigInt(row["FUserID"]);
                userPower.mChanegelsj = CommonFun.ConvertToBooleanStr(row["mChanegelsj"]);
                userPower.mClearMoney = CommonFun.ConvertToBooleanStr(row["mClearMoney"]);
                userPower.mReturnPrs = CommonFun.ConvertToBooleanStr(row["mReturnPrs"]);
                userPower.mOpenMoneyBox = CommonFun.ConvertToBooleanStr(row["mOpenMoneyBox"]);
                userPower.mOnePr_Agio = CommonFun.ConvertToBooleanStr(row["mOnePr_Agio"]);
                userPower.mCancelOrder = CommonFun.ConvertToBooleanStr(row["mCancelOrder"]);
                userPower.mPrinBjMore = CommonFun.ConvertToBooleanStr(row["mPrinBjMore"]);
                userPower.mSystemSetup = CommonFun.ConvertToBooleanStr(row["mSystemSetup"]);
                userPower.mDeleteARow = CommonFun.ConvertToBooleanStr(row["mDeleteARow"]);
                userPower.mHanguppOrder = CommonFun.ConvertToBooleanStr(row["mHanguppOrder"]);
                userPower.mPreferential = CommonFun.ConvertToBooleanStr(row["mPreferential"]);
                userPower.mOrderAgio = CommonFun.ConvertToBooleanStr(row["mOrderAgio"]);
                userPower.mPr_Largess = CommonFun.ConvertToBooleanStr(row["mPr_Largess"]);
                userPower.mChangeDjBz = CommonFun.ConvertToBooleanStr(row["mChangeDjBz"]);
                userPower.mPrinDataBz = CommonFun.ConvertToBooleanStr(row["mPrinDataBz"]);
                userPower.mUserKey_Bz = CommonFun.ConvertToBooleanStr(row["mUserKey_Bz"]);
                userPower.mPosOrderBz = CommonFun.ConvertToBooleanStr(row["mPosOrderBz"]);
                userPower.mField_01 = CommonFun.ConvertToBooleanStr(row["mField_01"]);
                userPower.mField_02 = CommonFun.ConvertToBooleanStr(row["mField_02"]);
                userPower.mField_03 = CommonFun.ConvertToBooleanStr(row["mField_03"]);
                userPower.mField_04 = CommonFun.ConvertToBooleanStr(row["mField_04"]);
                userPower.mField_05 = CommonFun.ConvertToBooleanStr(row["mField_05"]);
                userPower.mField_06 = CommonFun.ConvertToBooleanStr(row["mField_06"]);
                userPower.mField_07 = CommonFun.ConvertToBooleanStr(row["mField_07"]);
                userPower.mField_08 = CommonFun.ConvertToBooleanStr(row["mField_08"]);
                userPower.mPurAmount = CommonFun.ConvertToBooleanStr(row["mPurAmount"]);
                userPower.mDepositRet = CommonFun.ConvertToBooleanStr(row["mDepositRet"]);
                userPower.mFSeeStc = CommonFun.ConvertToBooleanStr(row["mFSeeStc"]);
                userPower.mDepositeTime = CommonFun.ConvertToBooleanStr(row["mDepositeTime"]);
                userPower.mMaxPr_Agio = CommonFun.ConvertToDecimal(row["mMaxPr_Agio"]);
                userPower.mMaxOrderJe = CommonFun.ConvertToDecimal(row["mMaxOrderJe"]);

                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Power/SavePowerInfo";
                string param = "mjson={0}&mSystemAppID={1}";
                string mjson = MniMngService.UserPowerModelToStr(userPower);

                ArrayList users = new ArrayList();
                Hashtable ht_user = new Hashtable();
                ht_user.Add("FItemID", CommonFun.ConvertToBigInt(row["FUserID"]));
                users.Add(ht_user);

                Hashtable ht = (Hashtable)PluSoft.Utils.JSON.Decode(mjson);
                ht["users"] = users;
                mjson = PluSoft.Utils.JSON.Encode(ht);

                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch (Exception ex) { }
            return null;
        }

        /// <summary>
        /// 用户信息保存
        /// </summary>
        public static Hashtable SetEmployeeInfo(Hashtable row, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                EmployeeModel employeeModel = new EmployeeModel();
                employeeModel.FItemID = CommonFun.ConvertToBigInt(row["FItemID"]);
                employeeModel.FName = CommonFun.ConvertToString(row["FName"]);
                employeeModel.FTelephone = CommonFun.ConvertToString(row["FTelephone"]);
                employeeModel.FPerType = CommonFun.ConvertToInt(row["FPerType"]);
                employeeModel.FRecPerType = CommonFun.ConvertToInt(row["FRecPerType"]);
                employeeModel.FTimePerType = CommonFun.ConvertToInt(row["FTimePerType"]);
                employeeModel.FSalePer = CommonFun.ConvertToDecimal(row["FSalePer"]);
                employeeModel.FRecPer = CommonFun.ConvertToDecimal(row["FRecPer"]);
                employeeModel.FTimePer = CommonFun.ConvertToDecimal(row["FTimePer"]);
                employeeModel.FState = CommonFun.ConvertToInt(row["FState"]);
                employeeModel.FUserID = CommonFun.ConvertToInt(row["FUserID"]);

                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Employee/SaveInfo";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.EmployeeModelToStr(employeeModel);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        public static string GetErpController(int type) {
            switch (type)
            {
                case 1:return "/GoodClass/delGoodClass";//商品分类
                case 2:return "/Member/RemoveMemberClass";//会员分类                    
                case 3:return "/Supply/RemoveSupplyInfo";//供应商资料                    
                case 4:return "/Member/RemoveRank";//会员等级
                case 5: return "/Goods/RemoveGoodRows";//商品资料
                case 6: return "/Member/removeInfo";//会员资料
                case 8: return "/Employee/RemoveInfo";//导购员资料
                case 9: return "/User/RemoveUserInfo";//收银员资料
                case 11: return "/Member/RemoveTimeRows";//次卡资料
                default: return null;
            }
        }

        /// <summary>
        /// 商品分类删除
        /// </summary>
        public static Hashtable DeleteInfo(string shop_appid,Int64 mFItemID, int type, ref int mResult, ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + GetErpController(type);
                string param = "json={0}&mSystemAppID={1}";
                Hashtable ht = new Hashtable();
                ht.Add("FItemID", mFItemID);                
                string mjson = PluSoft.Utils.JSON.Encode(ht); ;
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
                else {
                    string error = CommonFun.ConvertToString(ht_reust["error"]);
                    if (error != "") msg = error;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 会员等级保存
        /// </summary>
        public static Hashtable SetMemberRank(Model.BaseInfoModel baseInfoModel, 
            string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Member/SaveRankInfo";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.BaseInfoModelToStr(baseInfoModel,0);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 次卡分类
        /// </summary>
        public static Hashtable SetTimeType(Model.BaseInfoModel baseInfoModel,
            string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Member/SaveTimeClass";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.BaseInfoModelToStr(baseInfoModel, 0);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 次卡分类
        /// </summary>
        public static Hashtable SetTimeInfo(Hashtable row, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                Int64 mStockID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.StockID);
                TimeInfoModel timeInfoModel = new TimeInfoModel();
                timeInfoModel.FItemID = CommonFun.ConvertToBigInt(row["FItemID"]);
                timeInfoModel.FTimeClassID = CommonFun.ConvertToBigInt(row["FClassID"]);
                timeInfoModel.FNumber = CommonFun.ConvertToString(row["FNumber"]);
                timeInfoModel.FName = CommonFun.ConvertToString(row["FName"]);
                timeInfoModel.FRemark = CommonFun.ConvertToString(row["FRemark"]);
                timeInfoModel.FCardType = CommonFun.ConvertToInt(row["FCardType"]);
                timeInfoModel.FStockID = mStockID;
                timeInfoModel.FAmount = CommonFun.ConvertToDecimal(row["FAmount"]);
                timeInfoModel.FTime = CommonFun.ConvertToInt(row["FTime"]);
                timeInfoModel.FEffectDays = CommonFun.ConvertToInt(row["FEffectDays"]);
                timeInfoModel.FState = CommonFun.ConvertToInt(row["FState"]);
                timeInfoModel.FDate1 = CommonFun.ConvertToDate(row["FDate1"]).ToString();
                timeInfoModel.FDate2 = CommonFun.ConvertToDate(row["FDate2"]).ToString();
                timeInfoModel.FType = CommonFun.ConvertToInt(row["FType"]);

                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Member/SaveTimeInfo";
                string param = "json={0}&mSystemAppID={1}&jsongive={2}";
                string mjson = MniMngService.TimeInfoModelToStr(timeInfoModel);

                Hashtable ht = (Hashtable)PluSoft.Utils.JSON.Decode(mjson);
                ht["goodsRows"] = new ArrayList();
                ht["giveRows"] = new ArrayList();
                mjson = PluSoft.Utils.JSON.Encode(ht);

                param = string.Format(param, mjson, shop_appid,"");
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 供应商信息保存
        /// </summary>
        public static Hashtable SetSupplier(Model.SupplierModel supplierModel, string shop_appid, ref int mResult, ref string msg)
        {
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Supply/SaveSupplyInfo";
                string param = "json={0}&mSystemAppID={1}";
                string mjson = MniMngService.SupplierModelToStr(supplierModel);
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["msg"]);
                if (CommonFun.ConvertToInt(ht_reust["status"]) == 1)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 商品信息
        /// </summary>
        public static Hashtable GetGoodsInfo(string shop_appid, Int64 item_id)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_bi_GetGoodsInfo @FCorpID,@FItemID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@FItemID", SqlDbType.BigInt, item_id);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;                
                List<string> goods_imgs = new List<string>();
                string[] sFProImages = CommonFun.ConvertToString(ht["FProImage"]).Split('$');
                foreach (string str in sFProImages)
                {
                    if (!string.IsNullOrWhiteSpace(str))
                    {
                        goods_imgs.Add(SystemInfo.GetClusterHostUrl(shop_appid) + str);
                    }
                }
                ht["FGoodsImgList"] = goods_imgs;
                return ht;
            }
            return null;
        }

        /// <summary>
        /// 商品信息
        /// </summary>
        public static bool SetGoodsImg(string shop_appid, string sGoodsImgList, ref GoodsModel goodsModel)
        {
            try
            {
                string[] sFProImages = sGoodsImgList.Split(',');
                if (sFProImages.Count() > 0)
                {
                    int i = 0;
                    foreach (string str in sFProImages)
                    {
                        if (!string.IsNullOrWhiteSpace(str))
                        {
                            i += 1;
                            string mPath = str.Replace(SystemInfo.GetClusterHostUrl(shop_appid), "").Replace("http://", ""); 
                            if (i == 0) goodsModel.ImagFile0 = mPath;
                            if (i == 1) goodsModel.ImagFile1 = mPath;
                            if (i == 2) goodsModel.ImagFile2 = mPath;
                            if (i == 3) goodsModel.ImagFile3 = mPath;
                            if (i == 4) goodsModel.ImagFile4 = mPath;
                            if (i == 5) goodsModel.ImagFile5 = mPath;
                            if (i == 6) goodsModel.ImagFile6 = mPath;
                        }
                    }
                }
                else {
                    if (!string.IsNullOrEmpty(sGoodsImgList)) 
                        goodsModel.ImagFile0 = sGoodsImgList.Replace(SystemInfo.GetClusterHostUrl(shop_appid), "").Replace("http://", ""); 
                }
            }
            catch { }
            return true;
        }

        /// <summary>
        /// 通用信息
        /// </summary>
        public static Hashtable GetBaseItemInfo(string shop_appid, Int64 item_id,int type)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"EXEC pr_bi_GetBaseItemInfo @FItemID,@FItemType";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FItemID", SqlDbType.BigInt, item_id);
            par_list.Add("@FItemType", SqlDbType.VarChar, type.ToString());            
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist.Count > 0)
            {
                return datalist[0] as Hashtable;
            }
            return null;
        }

        /// <summary>
        /// 通用信息
        /// </summary>
        public static Hashtable SetShopInfo(string shop_appid, string FName, string FManager, 
            string FAddress, ref int mResult, ref string msg)
        {            
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"Update t_Corpration set FName = @FName,FManager = @FManager,
                            FAddress = @FAddress where FItemID = @FItemID
                            Update t_Stock set FName = @FName where FCorpID = @FItemID
                            Select FItemID,FName from t_Corpration where FItemID = @FItemID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FItemID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@FName", SqlDbType.VarChar, FName);
            par_list.Add("@FManager", SqlDbType.VarChar, FManager);
            par_list.Add("@FAddress", SqlDbType.VarChar, FAddress);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist.Count > 0)
            {
                mResult = 1;
                msg = "保存成功。";
                return datalist[0] as Hashtable;
            }
            return null;
        }

        /// <summary>
        /// 通用信息
        /// </summary>
        public static Hashtable SetSmsSignInfo(string shop_appid, string FSmsSign, ref int mResult, ref string msg)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"update t_CorpSMS set FSmsSign=@FSmsSign where FCorpID=@FCorpID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@FSmsSign", SqlDbType.VarChar, FSmsSign);
            DBHelper.ExeSQL(msql, par_list.Get_List(), connstr);
            mResult = 1;
            msg = "保存成功。";
            return null;
        }

        /// <summary>
        /// 通用信息
        /// </summary>
        public static Hashtable GetSystemInfo(string shop_appid, int type, ref int mResult, ref string msg)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            try
            {
                string msql = "Select {0} from t_SystemInfo";
                switch (type)
                {
                    case 1:
                        msql = string.Format(msql, "privacy");
                        break;
                    case 2:
                        msql = string.Format(msql, "about_us");
                        break;
                    case 3:
                        msql = string.Format(msql, "contact");
                        break;
                }
                ArrayList datalist = DBHelper.Select(msql,connstr);
                if (datalist != null && datalist.Count > 0)
                {
                    mResult = 1;
                    msg = "获取成功";
                    Hashtable ht = datalist[0] as Hashtable;
                    return ht;
                }
            }
            catch { 
            
            }
            return null;
        }

        /// <summary>
        /// 信息选择
        /// </summary>
        public static ArrayList SelectItems(string shop_appid, string type,int mClassID, string name, int page_index, int page_size, out int page_count)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = @"exec pr_ic_SelectItems @mPageSize,@mPageIndex,@mCorpID,@mBillType,@mName,@mClassID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mName", SqlDbType.VarChar, name);
            par_list.Add("@mBillType", SqlDbType.VarChar, type);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mClassID", SqlDbType.Int, mClassID);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
                return datalist;
            }
            page_count = 0;
            return null;
        }

        /// <summary>
        /// 信息选择
        /// </summary>
        public static ArrayList GetBaseItemList(string shop_appid, string type, string name, int page_index, int page_size, out int page_count)
        {
            page_count = 0;
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "Exec pr_bi_GetBaseItemList @mPageSize,@mPageIndex,@mCorpID,@mBillType,@mName,@mClassID";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mName", SqlDbType.VarChar, name);
            par_list.Add("@mBillType", SqlDbType.VarChar, type);
            par_list.Add("@mCorpID", SqlDbType.Int, mCorpID);
            par_list.Add("@mClassID", SqlDbType.Int, 0);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(),connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                page_count = Utils.GetPageCount(CommonFun.ConvertToInt(ht["FRowCount"]), page_size);
            }
            return datalist;
        }

        /// <summary>
        /// 公司选择
        /// </summary>
        public static ArrayList GetShopList(string shop_appid, string name,int page_index, int page_size)
        {
            var appModel = new SystemAppModel();
            //appModel.FMasterFlag = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.MasterFlag);
            appModel.FOwnerID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.OwnerID);
            appModel.FCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            appModel.FHostIP = SystemInfo.GetClusterHostIP(shop_appid);
            appModel.FDbSplit = SystemInfo.GetClusterSplit(appModel.FCorpID);
            appModel.FDtSplit = "";
            appModel.FModifyTime = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.ModifyTime);
            appModel.FLoginDate = DateTime.Now.Day;
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = "Exec pr_bi_GetShopList @mOwnerID,@mName,@mPageSize,@mPageIndex";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mPageSize", SqlDbType.Int, page_size);
            par_list.Add("@mPageIndex", SqlDbType.Int, page_index);
            par_list.Add("@mName", SqlDbType.VarChar, name);
            par_list.Add("@mOwnerID", SqlDbType.Int, appModel.FOwnerID);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);

            ArrayList datalist_ret = new ArrayList();
            foreach (Hashtable row in datalist)
            {
                appModel.FCorpID = CommonFun.ConvertToInt(row["FItemID"]);
                appModel.FUserID = CommonFun.ConvertToInt(row["FUserID"]);
                appModel.FStockID = CommonFun.ConvertToBigInt(row["FStockID"]);
                appModel.FParentID = CommonFun.ConvertToInt(row["FParentID"]);
                string sSystemAppID = SystemInfo.SetSystemAppID(appModel);
                row["shop_appid"] = sSystemAppID;

                datalist_ret.Add(row);
            }

            return datalist_ret;
        }

        /// <summary>
        /// 添加普通商品订单
        /// </summary>
        public static Hashtable AddWarehous(string shop_appid,Int64 mKinterID, int sSupplierID , string remark,
            int mDatatype, ArrayList detailed, ref int mResult,ref string msg)
        {
            if (detailed == null) return null;
            Hashtable ht_master = new Hashtable();
            ht_master["KInterID"] = mKinterID;
            ht_master["FBillNo"] = "";
            ht_master["FSystemAppID"] = shop_appid;
            ht_master["FSupplyID"] = sSupplierID;
            ht_master["FStockID"] = 0;
            ht_master["KInterID_Pur"] = 0;
            ht_master["FBillNO_Pur"] = 0;
            ht_master["FDate"] = System.DateTime.Now.ToString("yyyy-MM-dd");
            ht_master["FDataType"] = mDatatype;
            ht_master["FRemark"] = remark;
            ArrayList arrayList = new ArrayList();

            foreach (Hashtable row in detailed)
            {
                Hashtable ht_detail = new Hashtable();
                ht_detail["mFItemID"] = CommonFun.ConvertToInt(row["FGoodsID"]);
                ht_detail["mFQty"] = CommonFun.ConvertToDecimal(row["FQty"]);
                ht_detail["mFSetQty"] = 0;
                ht_detail["mFPrice"] = CommonFun.ConvertToDecimal(row["FPrice"]);
                ht_detail["mFAmount"] = CommonFun.ConvertToDecimal(row["FPrice"])* CommonFun.ConvertToDecimal(row["FQty"]);
                ht_detail["mFRemark"] = "";
                ht_detail["mFEntryID_Pur"] = 0;
                ht_detail["mFInterID_Pur"] = 0;
                ht_detail["mFBillNo_Pur"] = "";
                ht_detail["mFBatchNumber"] = "";
                ht_detail["mFQty_Pur"] = 0;
                ht_detail["mFWarranty"] = "";
                ht_detail["mFMakedate"] = "";
                arrayList.Add(ht_detail);
            }
            ht_master["goods_list"] = arrayList;
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Bill/SaveBillData_Ins";
                //url = "http://localhost:13065" + "/Bill/SaveBillData_Ins";
                string param = "mdatajson={0}";
                string mjson = CommonFun.GetValidJson(PluSoft.Utils.JSON.Encode(ht_master));
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["Status"]);
                if (CommonFun.ConvertToInt(ht_reust["id"]) > 0)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch (Exception ex) {
                msg = ex.Message;
            }
            return null;
        }

        /// <summary>
        /// 添加普通商品订单
        /// </summary>
        public static Hashtable AddInventory(string shop_appid, Int64 mKinterID, string remark,
            ArrayList detailed, ref int mResult, ref string msg)
        {
            if (detailed == null) return null;
            Int64 mStockID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.StockID);
            Int64 mUserID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);
            Hashtable ht_master = new Hashtable();
            ht_master["KInterID"] = mKinterID;
            ht_master["FBillNo"] = "";
            ht_master["FSystemAppID"] = shop_appid;
            ht_master["FBillerID"] = mUserID;            
            ht_master["FStockID"] = mStockID;
            ht_master["FDataType"] = 4;
            ht_master["FDate"] = System.DateTime.Now.ToString("yyyy-MM-dd");
            ht_master["FRemark"] = remark;
            ht_master["FContent"] = "";
            ArrayList arrayList = new ArrayList();

            foreach (Hashtable row in detailed)
            {
                Hashtable ht_detail = new Hashtable();
                ht_detail["mFInterID"] = 0;
                ht_detail["mFRowNo"] = 0;
                ht_detail["mFItemID"] = CommonFun.ConvertToInt(row["FGoodsID"]);
                ht_detail["mFQty"] = CommonFun.ConvertToDecimal(row["FQty"]);
                ht_detail["mFPrice"] = 0;
                ht_detail["mFAmount"] = 0;
                ht_detail["mFRemark"] = "";
                arrayList.Add(ht_detail);
            }
            ht_master["goods_list"] = arrayList;
            try
            {
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Bill/SaveBillData_Stc";
                //url = "http://localhost:13065" + "/Bill/SaveBillData_Stc";
                string param = "mdatajson={0}";
                string mjson = CommonFun.GetValidJson(PluSoft.Utils.JSON.Encode(ht_master));
                param = string.Format(param, mjson, shop_appid);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["Status"]);
                if (CommonFun.ConvertToInt(ht_reust["id"]) > 0)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return null;
        }

        public static Hashtable UploadImage(string shop_appid, string imgPath, string fPara,ref int mResult, ref string msg)
        {
            try
            {
                //string uploadUrl = "http://localhost:13065/Upload/UploadFile?mSystemAppID=" + shop_appid                
                string uploadUrl = SystemInfo.GetClusterHostUrl(shop_appid) + "/Upload/UploadFile?mSystemAppID=" + shop_appid;

                HttpWebRequest request = WebRequest.Create(uploadUrl) as HttpWebRequest;
                request.AllowAutoRedirect = true;
                request.Method = "POST";

                string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
                request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
                byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
                byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

                int pos = imgPath.LastIndexOf("/");
                string fileName = imgPath.Substring(pos + 1);

                //请求头部信息
                StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"" 
                    + fPara + "\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());

                FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
                byte[] bArr = new byte[fs.Length];
                fs.Read(bArr, 0, bArr.Length);
                fs.Close();

                Stream postStream = request.GetRequestStream();
                postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                postStream.Write(bArr, 0, bArr.Length);
                postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
                postStream.Close();

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream instream = response.GetResponseStream();
                StreamReader sr = new StreamReader(instream, Encoding.UTF8);
                string content = sr.ReadToEnd();
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(content);
                if (CommonFun.ConvertToInt(ht_reust["status"]) > 0)
                {
                    mResult = 1;
                    msg = "文件上传成功";
                }

                return ht_reust;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 单据审核
        /// </summary>
        public static Hashtable SetBillAudit(string shop_appid, Int64 bill_id, int bill_type,ref int mResult,ref string msg)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            Int64 mUserID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            //SystemInfo.writeServiceLog(connstr, "", "2408", "1", DBHelper.ConnMaster);
            string msql = @"exec pr_ic_CheckBillData @mKInterID,@mUserID,@mCoprID,@mDataType";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mKInterID", SqlDbType.BigInt, bill_id);
            par_list.Add("@mUserID", SqlDbType.BigInt, mUserID);
            par_list.Add("@mCoprID", SqlDbType.Int, mCorpID);
            par_list.Add("@mDataType", SqlDbType.VarChar, bill_type);
            ArrayList datalist = DBHelper.Select(msql, par_list.Get_List(), connstr);
            if (datalist != null && datalist.Count > 0)
            {
                Hashtable ht = datalist[0] as Hashtable;
                Int64 mCheckBillID = CommonFun.ConvertToInt(ht["KInterID"]);
                if (mCheckBillID == bill_id)
                {
                    mResult = 1;
                    msg = "单据审核成功!";                    
                }
                else
                {
                    mResult = 0;
                    msg = CommonFun.ConvertToString(ht["FStatus"]);
                }
                return datalist[0] as Hashtable;
            }
            return null;
        }

        /// <summary>
        /// 信息选择
        /// </summary>
        public static Hashtable GetBillData(string shop_appid, int bill_type, Int64 bill_id)
        {
            try
            { 
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Bill/GetBillData";
                string param = "mSystemAppID={0}&mBillID={1}&mDataType={2}";
                param = string.Format(param, shop_appid, bill_id, bill_type);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                return ht_reust;
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 信息选择
        /// </summary>
        public static Hashtable GetSysGoodByCode(string shop_appid, string mCode, ref int mResult, ref string msg)
        {
            try
            {
                string url = "http://sysgoods.pos08.com/Home/GetSysGoodByCode";
                string param = "mSystemAppID={0}&mCode={1}";
                param = string.Format(param, shop_appid, mCode);
                string result = MniMngService.HttpPostData(url, param);
                if (!string.IsNullOrEmpty(result))
                {
                    mResult = 1;
                    msg = "获取成功";
                }
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                //SystemInfo.writeServiceLog(result, shop_appid, "20221114", "1");
                return ht_reust;
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 完成盘点
        /// </summary>
        public static Hashtable SetInventoryAudit(string shop_appid, int type, ref int mResult, ref string msg)
        {
            try
            {
                int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
                string connstr = SystemInfo.GetClusterConn(shop_appid);
                string url = SystemInfo.GetClusterHostUrl(shop_appid) + "/Bill/CheckStockBill";
                //url = "http://localhost:13065" + "/Bill/CheckStockBill";
                string param = "mdatajson={0}";
                string mjson = MniMngService.GetCheckBillJson(shop_appid, type, connstr);
                if (mjson == "") {
                    msg = "没有可完成的盘点数据。";
                    mResult = 0;
                    return null;
                }
                param = string.Format(param, mjson);
                string result = MniMngService.HttpPostData(url, param);
                Hashtable ht_reust = (Hashtable)PluSoft.Utils.JSON.Decode(result);
                msg = CommonFun.ConvertToString(ht_reust["Status"]);
                if (CommonFun.ConvertToInt(ht_reust["id"]) > 0)
                {
                    mResult = 1;
                    return ht_reust;
                }
            }
            catch { }
            return null;
        }

        //保存扫码支付订单SaveIPay
        public static string SaveRenewalsOrder(int FCorpID, int FUserID,decimal Money,int mYears, ref string msg,int type,string shop_appid)
        {
            try
            {
                int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.TopCorpID);
                string connstr = SystemInfo.GetClusterConn(shop_appid);
                string msql = string.Format(@"exec Pr_GetBillOrderNoMore 5,{0},{1}", FCorpID, FUserID);
                DataTable dt_order = DBHelper.GetDataTable(msql, connstr);
                if ((dt_order == null) || (dt_order.Rows.Count == 0))
                {
                    msg = "充值生成订单失败";
                    return "";
                }
                string mName = "小程序商户续费";
                if (type == 2) mName = string.Format("短信服务费:{0}元,{1}条短信", Money, mYears);
                string BillNo = CommonFun.ConvertToString(dt_order.Rows[0]["FBillNo"]);
                int pos = 1;
                //if (server > 0) pos += 1;
                //if (pos < 0 && server > 0) pos = 1;
                string sql = @"insert into y_iPay(FCorpID,FBillNo,FMoney,FProductName,FYear,FPay,FCreateTime,"
                        + "FPos,FBServer,FTrade_no,FUserID,FAssType,FCartJson,FType,FMall)values(@CorpID,@BillNo,@Money,"
                        + "@Name,@Year,@FPay,getdate(),@Pos,@server,@tradeNo,@UserID,@AssType,@cartJson,@FType,1)";
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@BillNo", SqlDbType.VarChar, BillNo);
                par_list.Add("@Money", SqlDbType.Decimal, Money);
                par_list.Add("@Name", SqlDbType.VarChar, mName);
                par_list.Add("@Year", SqlDbType.Int, mYears);
                par_list.Add("@FPay", SqlDbType.Int, 0);//Money>0?0:1);
                par_list.Add("@CorpID", SqlDbType.Int, FCorpID);
                par_list.Add("@UserID", SqlDbType.BigInt, FUserID);
                par_list.Add("@Pos", SqlDbType.Int, pos);
                par_list.Add("@server", SqlDbType.Int, 1);
                par_list.Add("@tradeNo", SqlDbType.VarChar, "");
                par_list.Add("@AssType", SqlDbType.Int, 0);
                par_list.Add("@cartJson", SqlDbType.VarChar, "");
                par_list.Add("@FType", SqlDbType.Int, type);
                DBHelper.ExeSQL(sql, par_list.Get_List(), connstr);
                return BillNo;
            }
            catch (Exception e)
            {
                msg = e.Message;
                return "";
            }
        }

        /// <summary>
        /// 充值续费
        /// </summary>
        public static ArrayList GetRenewals(string shop_appid)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = string.Format("exec pr_bi_SetCorpProdcutAuto {0} exec pr_bi_GetCorpAppCenter {0},'',1", mCorpID);
            ArrayList datalist = DBHelper.Select(msql,connstr);

            ArrayList datalist_ret = new ArrayList();            
            foreach (Hashtable row in datalist)
            {
                bool mCheckFlag = false;
                if (CommonFun.ConvertToInt(row["FAppType"]) == 0) mCheckFlag = true;
                row["checked"] = mCheckFlag;
                datalist_ret.Add(row);
            }
            return datalist_ret;
        }

        /// <summary>
        /// 短信充值信息
        /// </summary>
        public static Hashtable GetSmsData(string shop_appid)
        {
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string connstr = SystemInfo.GetClusterConn(shop_appid);
            string msql = string.Format("exec pr_bi_GetCorpSMS {0}", mCorpID);
            ArrayList datalist = DBHelper.Select(msql, connstr);

            if (datalist.Count == 0) return null;
            Hashtable ht_ret = datalist[0] as Hashtable;
            ArrayList list_detail = new ArrayList();
            list_detail.Add(GetSmsDetail(1, "100元",1250,100,"1000条"));
            list_detail.Add(GetSmsDetail(2, "200元", 2600, 200, "2500条+赠送100条"));
            list_detail.Add(GetSmsDetail(3, "300元", 3900, 300, "3750条+赠送150条"));
            ht_ret["detail"] = list_detail;
            return ht_ret;
        }

        public static Hashtable GetSmsDetail(int FItemID, string FName, int FQty, int FAmount, string FExplain) 
        {
            Hashtable ht = new Hashtable();
            ht.Add("FItemID", FItemID);
            ht.Add("FName", FName);
            ht.Add("FQty", FQty);            
            ht.Add("FAmount", FAmount);
            ht.Add("FExplain", FExplain);
            return ht;
        }

        /// <summary>
        /// 定时轮询查单ArrayList rows, int FCorpID, int FUserID,decimal Money,ref string msg
        /// </summary>
        public static void SetRepeatQueryPay(ArrayList rows, string order_no, int business_id,int FYears,int type,string shop_appid)
        {
            //支付的订单类型，1：商城订单，1：从订单列表来的商城订单,11：会员充值
            Thread newThread = new Thread(() => 
            SetRepeatQueryPay_Thread(rows, order_no, business_id,FYears, type, shop_appid),1024 * 512);
            newThread.Name = String.Format("Thread{0}", order_no);
            newThread.Start();
        }

        /// <summary>
        /// 定时轮询查单
        /// </summary>
        public static void SetRepeatQueryPay_Thread(ArrayList rows, string order_no, int business_id,int FYears,int type,string shop_appid)
        {            
            bool bLoop = true;
            int mSleepTime = 10000;
            int mTimCount = 0;
            try
            {
                Thread.Sleep(mSleepTime);
                while (bLoop)
                {
                    Hashtable respData = new Hashtable();
                    bool bResult = SxfPayCommon.Query(order_no, business_id, ref respData);
                    if (bResult)
                    {
                        string transactionId = CommonFun.ConvertToString(respData["transactionId"]);
                        bLoop = false;//YunShop.AppCenter.SetSoftwareService
                        if (type == 2)
                        {
                            PaySMS(shop_appid,order_no, transactionId);
                        } else {
                            SetSoftwareService(rows, order_no, business_id, transactionId, FYears);
                        }                        
                        //UpdateOrderStatus(sql_conn, sql_conn_manage, order_no, pay_type, appid, out error);
                    }
                    if (mTimCount > 12) bLoop = false;
                    mTimCount += 1;
                    Thread.Sleep(mSleepTime);
                }
            }
            catch (Exception err)
            {
                //WriteErrMsg(methodName, err.Message, "");
            }
        }

        public static bool PaySMS(string shop_appid,string BillNo, string payID)
        {
            try
            {
                int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
                string connstr = SystemInfo.GetClusterConn(shop_appid);
                string sql = @"exec pr_bi_RechargeSMS @FBillNo,@FPayID";
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@FBillNo", SqlDbType.VarChar, BillNo);
                par_list.Add("@FPayID", SqlDbType.VarChar, payID);
                DataTable dt = DBHelper.GetDataTable(sql, par_list.Get_List(), connstr);

                if (dt == null)
                    return false;
                if (dt.Rows.Count == 0)
                    return false;
                if (CommonFun.ConvertToInt(dt.Rows[0]["retvalue"]) > 0)
                    return true;
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static bool SetSoftwareService(ArrayList rows, string order_no, int mCorpID, string transactionId, int years) 
        {
            try
            {
                long mFPayID = 0;
                if (!ExistsNoPayItem(order_no, mCorpID,ref mFPayID)) return false;
                string connstr = SystemInfo.GetClusterConn(mCorpID);
                List<DBHelper.CommandInfo> commandlist = new List<DBHelper.CommandInfo>();
                string sql = string.Format(@"declare @time datetime
                        select @time=case when FEndTime<=getdate() then getdate() else FEndTime end from t_corpration where FItemID={4}
                        update y_iPay set FPay=1,FPayID='{1}',FPayTime=getdate() where FItemID={0}
                        update t_corpration set FEndTime=dateadd({2},{3},@time) where FItemID={4}
                        ", mFPayID, transactionId, "year", years, mCorpID);
                DBHelper.CommandInfo info = new DBHelper.CommandInfo(sql, null);
                commandlist.Add(info);
                //DBHelper.ExeSQL(sql);

                sql = @"declare @FDetailID bigint  declare @endtime datetime
                        exec dbo.pr_bi_GetMaxAutoID 't_CorpProductDetail','FDetailID',1,@FDetailID out 
                        select @endtime= case when isnull(max(FEndDate),getdate())<=getdate() then getdate() else max(FEndDate) end 
                        from t_CorpProduct where FCorpID=@FCorpID and FProductID=@FProductID 
                        insert into t_CorpProductDetail(FDetailID,FCorpID,FPayID,FProductID,FDate,FEndDate) 
                        values(@FDetailID,@FCorpID,@FPayID,@FProductID,getdate(),dateadd({0},@FTime,@endtime) )                            
                        update t_CorpProduct set FDetailID=@FDetailID,FEndDate=dateadd({0},@FTime,@endtime) 
                        where FCorpID=@FCorpID and FProductID=@FProductID ";
                string msql = "";
  
                foreach (Hashtable item in rows)
                {
                    if (CommonFun.ConvertToBoolean(item["checked"]))
                    {
                        int FItemID = CommonFun.ConvertToInt(item["FItemID"]);
                        SqlParameterList par_list = new SqlParameterList();
                        par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
                        par_list.Add("@FPayID", SqlDbType.BigInt, mFPayID);
                        par_list.Add("@FProductID", SqlDbType.BigInt, FItemID);
                        par_list.Add("@FTime", SqlDbType.Int, years);
                        sql = string.Format(sql, "year");
                        DBHelper.CommandInfo info1 = new DBHelper.CommandInfo(sql, par_list.Get_List());
                        commandlist.Add(info1);

                        if (FItemID == 2 || FItemID == 1 || FItemID == 4 || FItemID == 7)
                        {
                            msql = "insert into t_POSTerminal(FCorpID,FCreateTime,FEndTime) "
                                +"values(@FCorpID,getdate(),dateadd({0},@year,getdate()))";
                            msql = string.Format(msql, "year");
                            par_list = new SqlParameterList();
                            par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
                            par_list.Add("@year", SqlDbType.Int, years);
                            info = new DBHelper.CommandInfo(msql, par_list.Get_List());
                            commandlist.Add(info);
                        }
                        if (FItemID == 6)
                        {
                            msql = string.Format(@"insert into t_POSTerminal(FCorpID,FCreateTime,FEndTime,FTerminalNo) 
                                        values(@FCorpID,getdate(),dateadd({0},@year,getdate()),'MobileTerminal')", "year");
                            par_list = new SqlParameterList();
                            par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
                            par_list.Add("@year", SqlDbType.Int, years);
                            info = new DBHelper.CommandInfo(msql, par_list.Get_List());
                            commandlist.Add(info);
                        }
                        if (FItemID == 24)
                        {
                            msql = string.Format(@"insert into t_POSTerminal(FCorpID,FCreateTime,FEndTime,FTerminalNo) 
                                        values(@FCorpID,getdate(),dateadd({0},@year,getdate()),'MarketingTerminal')", "year");
                            par_list = new SqlParameterList();
                            par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
                            par_list.Add("@year", SqlDbType.Int, years);
                            info = new DBHelper.CommandInfo(msql, par_list.Get_List());
                            commandlist.Add(info);
                        }
                        if (FItemID == 25)
                        {
                            msql = string.Format(@"insert into t_POSTerminal(FCorpID,FCreateTime,FEndTime,FTerminalNo) 
                                        values(@FCorpID,getdate(),dateadd({0},@year,getdate()),'AndroidTerminal')", "year");
                            par_list = new SqlParameterList();
                            par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
                            par_list.Add("@year", SqlDbType.Int, years);
                            info = new DBHelper.CommandInfo(msql, par_list.Get_List());
                            commandlist.Add(info);
                        }
                    }
                }
                msql = @"exec pr_bi_SetCorpProdcutAuto @FCorpID";
                SqlParameterList par_list2 = new SqlParameterList();
                par_list2.Add("@FCorpID", SqlDbType.Int, mCorpID);
                DBHelper.CommandInfo info2 = new DBHelper.CommandInfo(msql, par_list2.Get_List());
                commandlist.Add(info2);
                DBHelper.ExecuteSqlTran(commandlist, connstr);
            }
            catch (Exception e)
            {
                //WxPayAPI.Log.Info("SetSoftwareService", e.Message);
                return false;
            }
            return true;
        }

        private static bool ExistsNoPayItem(string order_no,int mCorpID,ref long mPayID)
        {
            string sql = "select FItemID from y_ipay where FBillNo=@FBillNo and FCorpID =@FCorpID and FPay=0";
            string connstr = SystemInfo.GetClusterConn(mCorpID); 
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FBillNo", SqlDbType.VarChar, order_no);
            par_list.Add("@FCorpID", SqlDbType.Int, mCorpID);
            DataTable dt = DBHelper.GetDataTable(sql, par_list.Get_List(), connstr);
            if (dt == null || dt.Rows.Count == 0) return false;
            if (dt.Rows.Count == 1)
            {
                mPayID = CommonFun.ConvertToBigInt(dt.Rows[0]["FItemID"]);
                return true;
            }
            else return false;
        }

        public static bool OnUserExists(string shop_appid)
        {
            int mUserID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);
            long mModifyTime = SystemInfo.GetSystemAppBigID(shop_appid, SystemAppID.ModifyTime);
            //string sql = "select FItemID from t_User where FItemID = @FItemID and FModifyTime = @FModifyTime";
            string sql = "select FItemID from t_User where FItemID = @FItemID and FModifyTime = @FModifyTime";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@FItemID", SqlDbType.Int, mUserID);
            par_list.Add("@FModifyTime", SqlDbType.BigInt, mModifyTime);
            DataTable dt = DBHelper.GetDataTable(sql, par_list.Get_List(), DBHelper.ConnMaster);
            if (dt == null || dt.Rows.Count == 0) return false;
            return true;
        }

    }
}
